//
//  NSDictionary+ACPEmptyDict.m
//  AutoCreatePropertyDemo
//
//  Created by Hongwei Wang on 2017/11/29.
//  Copyright © 2017年 puhui. All rights reserved.
//
#import "NSArray+ACPJsonArray.h"
#import "NSDictionary+ACPEmptyDict.h"

@implementation NSDictionary (ACPEmptyDict)

- (BOOL)acp_is_empty {
    BOOL isempty = NO;
    if (!self) {
        isempty = YES;
    } else if ([self isEqualToDictionary:@{}]) {
        isempty = YES;
    }
    return isempty;
}

- (NSString *)jsonString {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];

    NSString *jsonString = @"";

    if (! jsonData) {
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  //去除掉首尾的空白字符和换行字符

    return [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}

- (NSString *)dictString {
    NSString *string = @"";
    for (NSString *key in self.allKeys) {
        id obj = [self objectForKey:key];
        if ([obj isKindOfClass:[NSArray class]]) {
            string = [string stringByAppendingString:[obj arrayString]];
        } else if ([obj isKindOfClass:[NSDictionary class]]) {
            string = [string stringByAppendingString:[obj dictString]];
        } else {
            string = [@[key, [NSString stringWithFormat:@"%@", obj]] componentsJoinedByString:@""];
        }
    }
    NSString *str = [NSString stringWithFormat:@"{%@}", string];
    return str;
}

@end
